rm(list=ls())

##############################################################################
# APPENDIX B2 TABLE 1
##############################################################################

# load packages
#library(AER)
#library(kableExtra)

# load data 
load("data/eurepoc_year_attacks.RData")
load("data/zoneh_year_data.RData")

##############
# RUN MODELS #
##############

### dispersion test 1: ITU Annual 
dispersion_check1 <- glm(attacks ~ treat + factor(country_spell) + percent_usage, family = poisson, 
                         data = eurepoc_year_data)
#summary(dispersion_check1)

dispersion_check1$deviance/dispersion_check1$df.residual
# rule of thumb ratio of deviance to df should be 1
# this first check suggests no overdispersion for ITU annual data

dispersiontest(dispersion_check1) -> check1 # Cameron & Trivedi (1990)
# c = 1.024575 
# fail to reject the null that true dispersion is not greater than 1 (not overdispersed)

### dispersion test 2: ITU Monthly 

#dispersion_check2 <- glm(attacks ~ election + factor(country_spell), family = poisson,
#data = itu_month_data)
#summary(dispersion_check2)

#dispersion_check2$deviance/dispersion_check2$df.residual

#dispersiontest(dispersion_check2) -> check2
# c = 1.031531  
# fail to reject the null that true dispersion is not greater than 1 (not overdispersed)

### dispersion test 3: Zone-H Annual

dispersion_check3 <- glm(attacks ~ election + factor(group_spell), family = poisson,
                         data = zoneh_year_data)

#summary(dispersion_check3)
#dispersion_check3$deviance/dispersion_check3$df.residual

dispersiontest(dispersion_check3) -> check3
# c = 7.826672  
# reject the null that true dispersion is not greater than 1 (overdispersed)

### dispersion test 4: Zone-H Monthly 
#dispersion_check4 <- glm(attacks ~ election + factor(group_spell), family = poisson,
#data = zoneh_month_data)

#summary(dispersion_check4)
#dispersion_check4$deviance/dispersion_check4$df.residual

#dispersiontest(dispersion_check4) -> check4
# c = 3.738563   
# reject the null that true dispersion is not greater than 1 (overdispersed)


################
# CREATE TABLE #
################

outcome <- c("ITU Annual Attacks", "Zone-H Annual Attacks")
dispersion <- c(round(check1$estimate[[1]], 3), round(check3$estimate[[1]], 3))
zscore <-c(round(check1$statistic[[1]], 3), round(check3$statistic[[1]], 3))
pvalue <- c(round(check1$p.value[[1]], 3), "< 0.001" )

test_table <- cbind(outcome, dispersion, zscore, pvalue)

test_table %>%
  kbl(col.names = c("Dependent Variable", "Dispersion Statistic (c)", "Z Score", "P Value" ), format = "latex") %>%
  kable_classic(full_width = F, html_font = "Cambria") -> appendB2_tab1

print(appendB2_tab1)

# script complete message 
print("appendB2_tab1 complete")


